package com.mrd.util;

import com.mrd.constant.SystemConstants;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 退税截止日期计算工具类
 *
 * @author Administrator
 */
public class TaxDeadLineDateBuilder {
    /**
     * 计算退税截止日期
     *
     * @param releaseDate 放行时间
     */
    public static Date getTaxDeadLineDate(Date releaseDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(releaseDate);
        cal.add(Calendar.MONTH, SystemConstants.TAX_DEADLINE_RANGE);
        return cal.getTime();
    }

    /**
     * 判断放行日期是否要做提示
     */
    public static boolean isTaxAlert(Date releaseDate, int alertDays) {
        Date deadLine = getTaxDeadLineDate(releaseDate);
        Date today = new Date();
        if (today.after(deadLine) || alertDays < 0) {
            return false;
        } else {
            long quot = getQuot(deadLine, today);
            if (quot <= alertDays) {
                return true;
            }
        }
        return false;
    }

    /**
     * JAVA 日期差的计算
     */
    private static long getQuot(Date time1, Date time2) {
        long quot = 0;
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = ft.parse(ft.format(time1));
            Date date2 = ft.parse(ft.format(time2));
            quot = date1.getTime() - date2.getTime();
            quot = quot / 1000 / 60 / 60 / 24;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return quot;
    }
}
